package com.michaelbulava.TFS.ui.controls.connect;


import com.intellij.ui.AddEditRemovePanel;
import com.intellij.ui.ToolbarDecorator;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.jni.NativesHelper;
import com.michaelbulava.TFS.persistence.IdeaPersistenceStoreProvider;
import com.michaelbulava.TFS.ui.dialogs.connect.AddServerDialog;
import com.microsoft.tfs.core.util.ServerURIUtils;
import com.microsoft.tfs.core.util.serverlist.*;
import org.jetbrains.annotations.Nullable;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mbulava on 6/15/2015.
 */
public class ServerListControl extends AddEditRemovePanel<ServerListConfigurationEntry> {

    private ServerListManager serverListManager;

    public interface ServerListConfigurationEntryAddedListener{
        void onServerListConfigurationEntryAdded(ServerListConfigurationEntry newEntry);
    }

    private ServerListConfigurationEntryAddedListener listener;

    public ServerListControl() {
        super(new ServerListTableModel() , getCurrentList());
        if (NativesHelper.isNativeSet()){
            serverListManager = ServerListManagerFactory.getServerListProvider(IdeaPersistenceStoreProvider.INSTANCE);
        }
        ToolbarDecorator.findEditButton(this).setVisible(false);
        super.getTable().setShowColumns(true);
    }

    public void setServerListConfigurationEntryAddedListener(ServerListConfigurationEntryAddedListener listener){
        this.listener = listener;
    }

    private static List<ServerListConfigurationEntry> getCurrentList(){
        List<ServerListConfigurationEntry> list = new ArrayList<ServerListConfigurationEntry>();
        if (NativesHelper.isNativeSet()){
            ServerListManager manager = ServerListManagerFactory.getServerListProvider(IdeaPersistenceStoreProvider.INSTANCE);
            list.addAll(manager.getServerList().getServers());
        }
        return list;
    }



    @Nullable
    @Override
    protected ServerListConfigurationEntry addItem() {
        ServerListConfigurationEntry value = null;
        AddServerDialog dialog = new AddServerDialog(this);
        dialog.show();
        if (dialog.isOK()){
            //Fire the callback.

            value = new ServerListConfigurationEntry(dialog.getServerURI().getHost(),
                    ServerListEntryType.CONFIGURATION_SERVER, dialog.getServerURI());
            if(listener != null && value != null){
                listener.onServerListConfigurationEntryAdded(value);
            }
            ServerList list = serverListManager.getServerList();
            list.add(value);
            serverListManager.setServerList(list);

            return value;
        } else {
            return null;
        }
    }

    @Override
    protected boolean removeItem(ServerListConfigurationEntry o) {
        if (serverListManager != null){
            int result = Messages.showYesNoDialog(this, MessageFormat.format(Messages.getString("ServerListControl.RemoveMessageFormat"),
                            new Object[]{o.getName()}),
                    Messages.getString("ServerListControl.RemoveMessageTitle"));
            if (result == Messages.YES){
                ServerList list = serverListManager.getServerList();
                list.remove(o);
                return serverListManager.setServerList(list);
            }
        }
        return false;
    }

    @Nullable
    @Override
    protected ServerListConfigurationEntry editItem(ServerListConfigurationEntry o) {
        return null;
    }

    private static class ServerListTableModel extends TableModel<ServerListConfigurationEntry>{

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Nullable
        @Override
        public String getColumnName(int columnIndex) {
            switch(columnIndex){
                case 0:
                    return "Name";
                case 1:
                    return "URL";
                default:
                    return null;
            }
        }

        @Override
        public Object getField(ServerListConfigurationEntry o, int columnIndex) {
            switch(columnIndex){
                case 0:
                    return o.getName();
                case 1:
                    return ServerURIUtils.normalizeURI(o.getURI()).toString();
                default:
                    return null;
            }
        }
    }
}



